﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WWX.Admin.IDAL;
using WWX.Dto;
using WWX.Dto.Search;
using WWX.Model;
using SuperJoe.Framework.UnitOfWorkSql;
using SuperJoe.Framework.Core;
using SuperJoe.Framework.Core.Exceptions;

namespace WWX.Admin.BLL
{
    public class RechargeBLL : BaseBLL<IRechargeDAL>
    {
        public List<RechargeAdminDto> GetPageList(RechargeAdminSearch search, int pageNo, int pageSize, out int total)
        {
            return this.dal.GetPageList(search, pageNo, pageSize, out total);
        }

        public void Add(RechargeAdminSaveDto dto)
        {
            var memberDAL = this.uow.ResolveDAL<IMemberDAL>();
            var member = memberDAL.GetByAccount(dto.Account);
            if (member == null)
            {
                ThrowHelper.ThrowFriendlyException("账户不存在");
            }

            var recharge = new Recharge();
            recharge.No = NoHelper.GetNo("R");
            recharge.Id = Guid.NewGuid().ToString();
            recharge.CreatedBy = SuperJoeContext.Current.UserId;
            recharge.CreatedTime = DateTime.Now;
            recharge.Amount = dto.Amount;
            recharge.ZAmount = dto.ZAmount;
            recharge.Date = dto.Date.Value;
            recharge.Remark = dto.Remark;

            recharge.MemberId = member.Id;

            var deal = new Deal();
            deal.No = NoHelper.GetNo("D");
            deal.Id = Guid.NewGuid().ToString();
            deal.MemberId = member.Id;
            deal.Money = recharge.Amount;
            deal.CreatedBy = SuperJoeContext.Current.UserId;
            deal.CreatedTime = DateTime.Now;
            deal.ObjectType = DealObjectType.Recharge;
            deal.ObjectId = recharge.Id;
            deal.AccountRemainCash = member.Cash + member.ZCash + recharge.Amount;

            Deal deal1 = null;
            if (recharge.ZAmount > 0)
            {
                deal1 = new Deal();
                deal1.Id = Guid.NewGuid().ToString();
                deal1.MemberId = member.Id;
                deal1.Money = recharge.ZAmount;
                deal1.CreatedBy = SuperJoeContext.Current.UserId;
                deal1.CreatedTime = deal.CreatedTime.AddSeconds(1);
                deal1.ObjectType = DealObjectType.Recharge_Preferencial;
                deal1.ObjectId = recharge.Id;
                deal1.No = deal1.CreatedTime.ToString("DyyyyMMddHHmmss");
                deal1.AccountRemainCash = deal.AccountRemainCash + recharge.ZAmount;
            }

            var dealDal = this.uow.ResolveDAL<IDealDAL>();

            this.uow.ExecuteInTran(() =>
            {
                this.dal.Add(recharge);
                this.uow.ResolveDAL<IMemberDAL>().UpdateCash(member.Id, member.Cash + recharge.Amount, member.ZCash + recharge.ZAmount);
                dealDal.Add(deal);
                if(deal1!=null)
                {
                    dealDal.Add(deal1);
                }
            });
        }
    }
}
